<#import "/WEB-INF/sys/js_css.html" as js>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>模版管理</title>
</head>
<body>
<!-- 工具条 -->
<div id="tools" style="padding:5px;height:auto">
	<a href="javascript:void(0)" id="btn_top_add" class="easyui-linkbutton" iconCls="icon-add" plain="true">上传模版</a>
</div>
<table id="datagrid" class="easyui-datagrid" data-options="singleSelect:true,fit:true,fitColumns:true,pagination:true,
	url:'${ctx}/sys/model/search',toolbar:'#tools',rownumbers : true,pageSize:15,pageList:[5,10,15,20]">
    <thead>
		<tr>
			<th data-options="field:'id',hidden:true">id</th><!-- hidden -->
			<th data-options="field:'action',align:'center',width:5,formatter:formatterAction">操作</th>
			<th data-options="field:'modelname',align:'center',width:20">模板密钥</th>
			<th data-options="field:'modeldesc',align:'center',width:45">模板描述</th>
			<th data-options="field:'model',align:'center',width:10">模板类型</th>
			<th data-options="field:'createname',align:'center',width:10">创建人</th>
			<th data-options="field:'createTime',align:'center',width:10">创建人时间</th>
		</tr>
	</thead>
</table>
<!-- 上传模版div -->
<div id="divpadd" class="easyui-window" title="上传模版" data-options="modal:true,collapsible:false,closed:true,minimizable:false,
	maximizable:false,width:325">
	<form id="topaddform" method="post" enctype="multipart/form-data">
		<input type="hidden" name="id" id="addid" value="" />
		<table class="table table-hover table-condensed">
			<tr>
				<td>模板类型：<select class="easyui-combobox" name="model" id="addmodel" data-options="panelHeight:'auto'">
								<option value="pc" selected>PC模版</option>
								<option value="phone">手机模版</option>
								<option value="pad">平板模版</option>
					</select></td>
			</tr>
			<tr>
				<td>选择文件：<input type="file" id="addmodelfile" name="modelfile" value="" onchange="modelfilter()" /></td>
			</tr>
			<tr>
				<td>模板描述：</td>
			</tr>
			<tr>
				<td><textarea cols="39" name="modeldesc" id="addmodeldesc" rows="4"></textarea></td>
			</tr>
			<tr>
				<td align="center"><a href="javascript:void(0)" class="easyui-linkbutton" id="btn_window_top_add" iconCls="icon-save" plain="true">确认上传</a></td>
			</tr>
		</table>
	</form>
</div>
</body>
</html>
<@js.easyui />
<script type="text/javascript">
jQuery(document).ready(function() {
	//添加顶级权限信息
	jQuery("#btn_top_add").click(function(){
		jQuery("#divpadd").window("open");
		jQuery("#topaddform").form("clear");
		jQuery("#addid").val("");
		jQuery("#addmodel").combobox("setValue", "pc");
	});
	//添加按钮事件
	jQuery("#btn_window_top_add").click(function(){
		jQuery("#topaddform").form({
			url:"${ctx}/sys/model/add",
			onSubmit:function(){
				if(jQuery("#addid").val()==""){
					if(!modelfilter())
					return false;
				}
				var servervali = false;
				jQuery.ajax({
		   			async:false,
		   			type:"post",
		   			dataType:"json",
		   			data:{id:jQuery("#addid").val(),modelname:jQuery("#addmodelname").val()},
		   			url:"${ctx}/sys/model/validate",
		   			success:function(date){
		   				if(!date.success){
		   					jQuery.messager.alert("信息",date.message,"error");
		   				}else
		   					servervali = true;
		   			}
		   		});
				return servervali;
			},
			success:function(data){
				if(!data){
					jQuery.messager.alert("信息","保存失败！","error");
					return;
				}
				jQuery.messager.alert("信息","保存成功！","info",function(){
					jQuery("#divpadd").window("close");
				});
				jQuery("#datagrid").datagrid("reload");
			}
		});
		//提交表单
		jQuery('#topaddform').submit();
	});
});
//缩略图上传限制
function modelfilter(){
	var file = document.getElementById("addmodelfile");
	var fileName = file.value;
	var file_typename = fileName.substring(fileName.lastIndexOf('.'), fileName.length);
	if (!(file_typename == '.zip')) {
		file.value="";
		jQuery.messager.alert("信息","请把模版文件打包为zip格式上传！");
		file.value="";
		return false;
	}
	return true;
}
//操作按钮显示
function formatterAction(value,row,index){
	return "<img title='修改模板' onclick='editfun(\""+row.id+"\")' src='${ctx}/js/easyui/themes/icons/pencil.png' />&nbsp;&nbsp;"+
	"<img title='删除此模板' onclick='deletefun(\""+row.id+"\")' src='${ctx}/js/easyui/themes/icons/edit_remove.png' />";
}
//编辑模版
function editfun(id){
	jQuery.ajax({
		async:false,
		type:"post",
		dataType:"json",
		data:{id:id},
		url:"${ctx}/sys/model/bean",
		success:function(date){
			if(!date.success){
				jQuery.messager.alert("信息",date.message,"error");
				return;
			}
			var bean = date.bean;
			jQuery("#divpadd").window("open");
			jQuery("#topaddform").form("clear");
			jQuery("#addid").val(bean.id);
			jQuery("#addmodeldesc").val(bean.modeldesc);
			jQuery("#addmodel").combobox("setValue", bean.model);
		}
	});
}
//删除模板
function deletefun(id){
	jQuery.messager.confirm("信息","删除此用户后关于此用户的所有功能将失效，您确定要删除？",function(arg){
		if(!arg)
			return;
		jQuery.ajax({
			async:false,
			type:"post",
			dataType:"json",
			data:{id:id},
			url:"${ctx}/sys/model/delete",
			success:function(date){
				if(!date.success){
					jQuery.messager.alert("信息",date.message,"error");
					return;
				}
				//刷新页面
				jQuery("#datagrid").datagrid("reload");
			}
		});
	});
}
</script>